#include "cmdHandle/CNetBase.h"
#include "CControlManage.h"
#include "config.h"
#include "CControlTool.h"

NETWORKSERVICE_BEGIN_NAMESPACE

// 所有定时器注册函数
std::list<std::function<void(void)> >* CNetBase::m_AllTimeInitFuncList = nullptr;
std::list<std::function<void(void)> >* CNetBase::m_AllTimeUninitFuncList = nullptr;

CNetBase::CNetBase(bool isStartTimer, int startTimer, int Interval, QObject *p)
:m_isStartTimer(isStartTimer), m_startTimer(startTimer), m_Interval(Interval)
,QObject(p)
{
    // 定时器句柄
    m_timer = nullptr;

	if(!m_AllTimeInitFuncList){
		m_AllTimeInitFuncList = new list<std::function<void(void)>>;
	}
	if(!m_AllTimeUninitFuncList){
		m_AllTimeUninitFuncList = new list<std::function<void(void)>>;
	}
    m_AllTimeInitFuncList->push_back(STD_BIND_FUNC(CNetBase::myInitTimer, this));
    m_AllTimeUninitFuncList->push_back(STD_BIND_FUNC(CNetBase::myUninitTimer, this));

}
CNetBase::~CNetBase()
{
    myUninitTimer();
}

void CNetBase::timerOut()
{
    qDebug("Continue pulling data");
    m_timer->setInterval(m_Interval*1000);
}


// 注册定时器函数
void CNetBase::myInitTimer()
{
	if(m_isStartTimer){
        if(!m_timer){
            m_timer = new QTimer(this);
        }
        connect(m_timer, &QTimer::timeout, this, &CNetBase::timerOut);
        m_timer->start(m_startTimer*1000);

    }
}

// 删除定时器
void CNetBase::myUninitTimer()
{
    if (m_timer)
    {
        m_timer->stop();
        delete m_timer;
        m_timer = nullptr;
    }

}
NETWORKSERVICE_END_NAMESPACE
